import Vue from 'vue'
import Vuex from 'vuex'

// ①安装插件 
Vue.use(Vuex)

//  ② 创建对象 
const store = new Vuex.Store({
    state: {   // 保存状态
        counter: 100,
        students: [
           {id:100,name:'hai',age:18} ,
           {id:101,name:'pai',age:20} ,
           {id:102,name:'xie',age:22} ,
           {id:103,name:'pi',age:24} ,

        ]
    },
    mutations: {
        // 定义方法
        increment(state) {
            state.counter++
        },
        decrement(state) {
            state.counter--
        },
    },
    actions: {

    },
    getters: {
        squareCounter(state) {
            return state.counter * state.counter
        },
        more20stu(state) {
            return state.students.filter(newarr =>newarr.age >= 20)
        },
        more20stuLength(state,getters) {
            return getters.more20stu.length
        },
        moreAgeStu(state) {
            return function (age) {
            return state.students.filter(newarr =>newarr.age >age)
                
            }
        }
    },
    modules: {

    },
})
// ③ 导出 Store 对象
export default store